package com.atguigu.IO;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class demo3 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的路径:");
        File root = new File(scanner.next());
        if (!root.isDirectory()){
            System.out.println("您输入的目录不存在");
            return;
        }
        System.out.println("请输入要搜索的关键词:");
        String dest=scanner.next();
        scanDir(root,dest);
    }

    private static void scanDir(File root, String dest) throws IOException {
        File[] files=root.listFiles();
        if(files==null){
            return;
        }
        for(File f:files){
            if(f.isDirectory()){
                scanDir(f,dest);
            }else{
                //判断文件名是否符合要求
                checkFile(f,dest);
            }
        }
    }

    private static void checkFile(File f, String dest) throws IOException {
        if(f.getName().contains(dest)){
            System.out.println("该关键字"+dest+"被"+f.getCanonicalPath()+"包含了 ,是否要删除？(Y/N)");
            Scanner scanner=new Scanner(System.in);
            String ch=scanner.next();
            if(ch.equals("Y")||ch.equals("y")){
                f.delete();
            }
        }
    }
}
